//
//  ViewController.m
//  综合练习03
//
//  Created by student on 16/3/1.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,strong)NSArray *codes;
@property (weak, nonatomic) IBOutlet UIView *uiview;
@property (nonatomic,assign)NSString *typename;
@property(nonatomic,strong)NSArray *images;
@property (nonatomic,assign)NSString *imagename;
@property(nonatomic,strong)NSMutableArray *codesnames;
@property(nonatomic,strong)NSMutableArray *count;
@end

@implementation ViewController

- (IBAction)sync:(id)sender
{
    self.codesnames=[[NSMutableArray alloc]init];
    self.count=[[NSMutableArray alloc]init];
    for(int i=0;i<8;i++)
    {
        [self.count addObject:@0];
    }
    for(UIView *v in self.uiview.subviews)
    {
        if([v isKindOfClass:[UIView class]])
        {
           UIView *view=v;
            for(UIView *vv in view.subviews)
            {
                if([vv isKindOfClass:[UIButton class]])
                {
                    int i=arc4random()%5;
                    self.typename=self.codes[i][@"chinese"];
                    self.images=self.codes[i][@"images"];
                    self.imagename=self.images[arc4random()%6];
                    self.codename.text=[NSString stringWithFormat:@"%@",self.typename];
                    [self.codesnames addObject:self.typename];
                    NSLog(@"%@",self.typename);

                    UIButton *btn=vv;
                    [btn setImage:[UIImage imageNamed:self.imagename] forState:UIControlStateNormal];
                    
                }
                else if ([vv isKindOfClass:[view class]])
                {
                    vv.hidden=YES;
                }
            }
        }
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.codesnames=[[NSMutableArray alloc]init];
    self.count=[[NSMutableArray alloc]init];
    for(int i=0;i<8;i++)
    {
        [self.count addObject:@0];
    }
    NSString *path=[[NSBundle mainBundle]pathForResource:@"codes" ofType:@"plist"];
    self.codes=[NSArray arrayWithContentsOfFile:path];
    CGFloat width=self.uiview.bounds.size.width/4;
    CGFloat height=self.uiview.bounds.size.height/2;
    
    
    for(int i=0;i<4;i++)
    {
        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(width*i, 0, width, height)];
        UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, width, height)];
        btn.tag=i+1;
        [btn addTarget:self action:@selector(press:) forControlEvents:UIControlEventTouchUpInside];
        UIView *chooseview=[[UIView alloc]initWithFrame:CGRectMake(width-20, height-20, 20, 20)];
        chooseview.tag=(i+1)*10;
        chooseview.backgroundColor=[UIColor greenColor];
        chooseview.hidden=YES;
        [view addSubview:btn];
        [view addSubview:chooseview];
        [self.uiview addSubview:view];
    }
    for(int i=4;i<8;i++)
    {
        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(width*(i-4), height, width, height)];
        UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, width, height)];
        btn.tag=i+1;
        [btn addTarget:self action:@selector(press:) forControlEvents:UIControlEventTouchUpInside];
        UIView *chooseview=[[UIView alloc]initWithFrame:CGRectMake(width-20, height-20, 20, 20)];
        chooseview.tag=(i+1)*10;
        chooseview.backgroundColor=[UIColor greenColor];
        chooseview.hidden=YES;
        [view addSubview:btn];
        [view addSubview:chooseview];
        [self.uiview addSubview:view];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)press:(UIButton *)sender
{
   
    for(UIView *v in self.uiview.subviews)
    {
        if([v isKindOfClass:[UIView class]])
        {
            UIView *view=v;
            for(UIView *vv in view.subviews)
            {
                    if(vv.tag==sender.tag*10)
                    {
                        vv.hidden=!vv.hidden;
                        if(vv.hidden==NO)
                        {
                            self.count[sender.tag-1]=@1;
                        }
                        else
                        {
                            self.count[sender.tag-1]=@0;
                        }
                    }
            }
        }
    }
}

- (IBAction)pressed:(id)sender
{
    int name=0;
    int color=0;
    for(int i=0;i<8;i++)
    {
        if([self.count[i] intValue]==1)
        {
            if([self.codesnames[i] isEqualToString:self.codename.text])
            {
                color++;
               
                
            }
        }
    }
    
    for(int i=0;i<8;i++)
    {
        NSLog(@"%@",self.codesnames[i]);
        if([self.codesnames[i] isEqualToString:self.codename.text])
        {
            name++;

        }
    }
    if(color==name && color!=0 && name!=0 && color!=8 && name!=8)
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"登录信息" message:@"登陆成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
        color=0;
        name=0;
    }
    else
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"登录信息" message:@"登陆失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
        color=0;
        name=0;
    }
    
}

@end
